package cn.itcast.item.service.impl;


import cn.itcast.feign.client.ItemClient;
import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.IfNode;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemService itemService;



    //分页
    @Override
    public PageDTO<Item> getlist(SearchItemDTO dto) {


        LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getBrand())){
            wrapper.eq(Item::getBrand,dto.getBrand());
        }
        if (StringUtils.isNotBlank(dto.getCategory())){
            wrapper.eq(Item::getCategory,dto.getCategory());
        }
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.eq(Item::getName,dto.getName());
        }
        if (dto.getBeginDate()!= null && dto.getEndDate()!= null){
            wrapper.between(Item::getCreateTime,dto.getBeginDate(),dto.getEndDate());
        }
        Page<Item> page = this.page(new Page<>(dto.getPage(),dto.getSize()),wrapper);
        return new PageDTO<>(page.getTotal(),page.getRecords());

    }

    //添加
    @Override
    public ResultDTO add(Item item) {
        int insert = itemMapper.insert(item);
        if (item != null){
            return ResultDTO.ok();
        }else {
            return ResultDTO.error();
        }
    }

    //上架与下架
    @Override
    public ResultDTO UpAndDown(String id, String status) {
        Item item = new Item();
        item.setId(Long.valueOf(id));
        item.setStatus(Integer.parseInt(status));
        boolean update = itemService.updateById(item);
        if (update == true){
            return ResultDTO.ok();
        }else {
            return ResultDTO.error();
        }
    }
    //修改
    @Override
    public ResultDTO updata(Item item) {
        itemService.updateById(item);
        if (item != null) {
            return ResultDTO.ok();
        } else
            return ResultDTO.error();
    }

}
